package com.nova.lfh.zxing;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;

import com.google.zxing.ResultPoint;
import com.journeyapps.barcodescanner.ViewfinderView;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者 ： 暗网
 * 创建时间：2017/3/2
 * 备注：
 */

public class RViewFinderView extends ViewfinderView {
	private Context mContext;
	private Drawable mScanGridBg;

	protected int scannerIndex = 0;//
	protected static final int MAX_SCANNER_INDEX = 30; //
	private final int QUADRANGLE = 20;//四边的宽度  20的倍数

	public RViewFinderView(Context context, AttributeSet attrs) {
		super (context, attrs);
		this.mContext = context;
		mScanGridBg = context.getResources ().getDrawable (R.mipmap.scan_ray_grid);
	}

	@Override
	public void onDraw(Canvas canvas) {
//		super.onDraw (canvas);  注释掉原有的
		//更改扫描背景 和 增加四个边角

		refreshSizes ();
		if (framingRect == null || previewFramingRect == null) {
			return;
		}

		Rect frame = framingRect;
		Rect previewFrame = previewFramingRect;

		int width = canvas.getWidth ();
		int height = canvas.getHeight ();

		// Draw the exterior (i.e. outside the framing rect) darkened
		paint.setColor (resultBitmap != null ? resultColor : maskColor);
		canvas.drawRect (0, 0, width, frame.top, paint);
		canvas.drawRect (0, frame.top, frame.left, frame.bottom + 1, paint);
		canvas.drawRect (frame.right + 1, frame.top, width, frame.bottom + 1, paint);
		canvas.drawRect (0, frame.bottom + 1, width, height, paint);


		/*------------增加四个边角------------------*/
		paint.setColor (resultPointColor);
		//左上角
		canvas.drawRect (frame.left - QUADRANGLE, frame.top - QUADRANGLE, frame.left + 3 * QUADRANGLE, frame.top, paint);
		canvas.drawRect (frame.left - QUADRANGLE, frame.top, frame.left, frame.top + 3 * QUADRANGLE, paint);

		//右上角
		canvas.drawRect (frame.right - 3 * QUADRANGLE, frame.top - QUADRANGLE, frame.right + QUADRANGLE, frame.top, paint);
		canvas.drawRect (frame.right, frame.top, frame.right + QUADRANGLE, frame.top + 3 * QUADRANGLE, paint);

		//左下角
		canvas.drawRect (frame.left - QUADRANGLE, frame.bottom, frame.left + 3 * QUADRANGLE, frame.bottom + QUADRANGLE, paint);
		canvas.drawRect (frame.left - QUADRANGLE, frame.bottom - 3 * QUADRANGLE, frame.left, frame.bottom, paint);
		//右下角
		canvas.drawRect (frame.right - 3 * QUADRANGLE, frame.bottom, frame.right + QUADRANGLE, frame.bottom + QUADRANGLE, paint);
		canvas.drawRect (frame.right, frame.bottom - 3 * QUADRANGLE, frame.right + QUADRANGLE, frame.bottom, paint);
		/*--------------------------------------*/
		if (resultBitmap != null) {
			// Draw the opaque result bitmap over the scanning rectangle
			paint.setAlpha (CURRENT_POINT_OPACITY);
			canvas.drawBitmap (resultBitmap, null, frame, paint);
		} else {

			// Draw a red "laser scanner" line through the middle to show decoding is active
			paint.setColor (laserColor);
			paint.setAlpha (SCANNER_ALPHA[scannerAlpha]);
			scannerAlpha = (scannerAlpha + 1) % SCANNER_ALPHA.length;
			/*-------------中心线更改为- 扫描图案-------------------*/
//            int middle = frame.height() / 2 + frame.top;
//			canvas.drawRect (frame.left + 2, middle - 1, frame.right - 1, middle + 2, paint);

			scannerIndex = (scannerIndex + 1) % MAX_SCANNER_INDEX;
			int middle;
			if (scannerIndex == 0) {
				middle = frame.hashCode () + frame.top;
			} else {
				middle = frame.height () * scannerIndex / MAX_SCANNER_INDEX + frame.top;
			}
			if (mScanGridBg != null) {
				mScanGridBg.setBounds (frame.left, frame.top, frame.right - 1, middle + 1);
				mScanGridBg.draw (canvas);
			}

	/*---------------------------------*/
			float scaleX = frame.width () / (float) previewFrame.width ();
			float scaleY = frame.height () / (float) previewFrame.height ();

			List<ResultPoint> currentPossible = possibleResultPoints;
			List<ResultPoint> currentLast = lastPossibleResultPoints;
			int frameLeft = frame.left;
			int frameTop = frame.top;
			if (currentPossible.isEmpty ()) {
				lastPossibleResultPoints = null;
			} else {
				possibleResultPoints = new ArrayList<> (5);
				lastPossibleResultPoints = currentPossible;
				paint.setAlpha (CURRENT_POINT_OPACITY);
				paint.setColor (resultPointColor);
				for (ResultPoint point : currentPossible) {
					canvas.drawCircle (frameLeft + (int) (point.getX () * scaleX),
							frameTop + (int) (point.getY () * scaleY),
							POINT_SIZE, paint);
				}
			}
			if (currentLast != null) {
				paint.setAlpha (CURRENT_POINT_OPACITY / 2);
				paint.setColor (resultPointColor);
				float radius = POINT_SIZE / 2.0f;
				for (ResultPoint point : currentLast) {
					canvas.drawCircle (frameLeft + (int) (point.getX () * scaleX),
							frameTop + (int) (point.getY () * scaleY),
							radius, paint);
				}
			}

			// Request another update at the animation interval, but only repaint the laser line,
			// not the entire viewfinder mask.
			postInvalidateDelayed (ANIMATION_DELAY,
					frame.left - POINT_SIZE,
					frame.top - POINT_SIZE,
					frame.right + POINT_SIZE,
					frame.bottom + POINT_SIZE);
		}


	}
}
